package cn.tedu.boot22.controller;

import cn.tedu.boot22.utils.DBUtils;
import cn.tedu.boot22.vo.Article;
import cn.tedu.boot22.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@Controller
public class ArticleController {

    /**
     * 发布文章业务
     * @param article
     * @return
     */
    @RequestMapping("/writeArticle")
    @ResponseBody
    public String writeArticle(Article article) {
        /*
            新建文章数据表
            create table article(id int primary key auto_increment, title varchar(50), author varchar(50), content varchar(50))
         */
        try (
                Connection conn = DBUtils.getConn();
        ) {
            String sql = "insert into article values(null,?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, article.getTitle());
            ps.setString(2, article.getAuthor());
            ps.setString(3, article.getContent());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "发表成功";
    }

    /**
     * 文章列表功能
     * @return
     */
    @RequestMapping("/showArticle")
    @ResponseBody
    public String showArticle() {
        ArrayList<Article> list = new ArrayList<>();
        try (
                Connection conn = DBUtils.getConn();
        ) {
            String sql = "select title,author,content from article";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                String title = rs.getString(1);
                String author = rs.getString(2);
                String content = rs.getString(3);
                Article article = new Article(title, author, content);
                list.add(article);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println(list.size());
        //遍历集合把里面的数据装进一个html里面
        String html = "<table border=1>";
        html += "<caption>文章列表</caption>";
        html += "<tr><th>标题</th><th>作者</th><th>内容</th></tr>";
        for (Article article : list) {
            html += "<tr>";
            html += "<td>" + article.getTitle() + "</td>";
            html += "<td>" + article.getAuthor() + "</td>";
            html += "<td>" + article.getContent() + "</td>";
            html += "</tr>";
        }
        html += "</table>";

        return html;
    }

    /**
     * 通过标题搜索文章
     * @param title
     * @return
     */
    @RequestMapping("inquireArticle")
    @ResponseBody
    public String inquireArticle(String title){
        ArrayList<Article> list = new ArrayList<>();
        try (
                Connection conn = DBUtils.getConn();
                ){
            String sql = "select title,author,content from article where title like ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            String selectTitle = "%"+title+"%"; //通过like %X% 搜索文章
            ps.setString(1,selectTitle);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                String titles = rs.getString(1);
                String author = rs.getString(2);
                String content = rs.getString(3);
                Article article = new Article(titles, author, content);
                list.add(article);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //遍历集合把里面的数据装进一个html里面
        String html = "<table border=1>";
        html += "<caption>文章列表</caption>";
        html += "<tr><th>标题</th><th>作者</th><th>内容</th></tr>";
        for (Article article : list) {
            html += "<tr>";

            html += "<td>" + article.getTitle() + "</td>";
            html += "<td>" + article.getAuthor() + "</td>";
            html += "<td>" + article.getContent() + "</td>";
            html += "</tr>";
        }
        html += "</table>";

        return html;
    }
}
